<?php
namespace app\admin\model;
use think\Model;
use think\Db;

class Order extends Model
{
    /**
     * [获取支付类型]
     * @return [int] ['0'=>'微信支付','1'=>'押金支付']
     */
    public function getTypeAttr($value, $data)
    {
        $get_data = ['0'=>'微信支付','1'=>'押金支付'];
        return $get_data[$data['type']];
    }

    public function getCreateTimeTurnAttr($value, $data)
    {
        return date('Y-m-d H:i:s', $data['create_time']);
    }

    /**
     * [获取支付状态]
     * @return [int] ['0'=>'已付款','1'=>'未付款']
     */
    public function getIsPayAttr($value,$data)
    {
        $get_data = ['0'=>'已付款','1'=>'未付款'];
        return $get_data[$data['isPay']];
    }

    /**
     * 获取订单状态
     * @return [int] ['订单状态  0--订单审核 1--订单成功 2--订单失败 3--取消订单 4--商家未接受订单']
     */
    public function getStatusAttr($value, $data)
    {
            $get_data = [
            '0'=>'审核中',
            '1'=>'已预约',
            '2'=>'健身中',
            '3'=>'待支付',
            '4'=>'已结束',
            '10'=>'已取消'
            ];
            return $get_data[$data['status']];
    }


    //查询每个场馆下的所有订单信息
    public function selectOrderList()
    {
        //先查询出所有场馆的信息
        $dataList = Db::name('place')->alias('p')
        ->join('gx_place_pic i', 'i.place_id = p.id')
        ->join('gx_cuser c','c.id = p.id')
        ->order('p.id desc')
        ->paginate('',false,page_param());


        return $dataList;
    }

    /**
     * 获取统计订单
     * @return [type] [description]
     */
    public function orderSum()
    {
        $res = Db::name('place')->alias('p')
                ->join('gx_order o o.place_id = p.id')
                ->join('gx_cuser c c.id = p.id')
                ->order('id desc')
                ->paginate('',false,page_param());
        return $res;
    }


    public function getOrderList($map = [], $order = 'id desc')
    {

        $field = 'order_number,user_id,isPay,type,place_id,status,order_time';
        $data = $this->where($map)->field($field)
            ->order($order)->paginate('3',false,page_param());

        $re = [];
        foreach ($data as $order) {
            $re[] = $order->toArray();
        }
        return $re;
    }

    /**
     * @param array $map
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function getOrderInfo($map = [])
    {
        if (!$map) return false;
        $field = 'order_no,amount,distribute,order_desc,consignee_name,consignee_mobile,consignee_address,send_state,is_pay,is_comment,order_state,create_time';
        $data = $this->where($map)->field($field)->find();
        if (!$data) return false;
        //$data['cart_list']=$data->hasManyThrough('VGuidAll','OrdersAccess','order_no','my_guid','order_no')->select();
        $data['cart_list'] = $this->getOrderAccessList($data['order_no']);
        return $data;

    }

    /*********根据场馆的ID 来查询 该场馆下所有的订单信息************/
    public function getInfo()
    {
        $id = input('id');
        $where = ['id'=>['in',$id]];
        $res = Db::name('appointment_gym')->alias('a')
            ->join('gx_order o',' o.id = a.order_id')
            ->join('gx_place p',' p.id = a.place_id')
            ->join('gx_cuser m',' m.id = a.user_id')
            ->join('gx_cuser_info c',' c.uid=a.user_id')
            ->where(['a.order_id'=>$id])
            ->paginate('',false,page_param());
            // dump($res);die;
        return $res;
    }

    /*******根据订单号来查询订单详情信息********/
    public function getOrderDetails()
    {
        $order_no = input('order_number');
        $res = Db::name('appointment_gym')->alias('a')
                ->join('gx_order o', 'o.id = a.order_id')
                // ->join('gx_place p',' p.id = a.place_id')
                // ->join('gx_cuser m',' m.id = a.user_id')
                // ->join('gx_cuser_info c',' c.uid=a.user_id')
                ->where(['o.order_number'=>$order_no])
                ->paginate('',false,page_param());
        return $res;
    }

    /*
    * 关联
    */
    public function orderAccess(){
        $this->hasMany('OrdersAccess','order_number','order_number');
    }
    /**
     * 视图查询
     * Power by Mikkle
     * QQ:776329498
     * @param $order_no
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getOrderAccessList($order_no){
        return Db::view('my_orders_access','id,order_no,my_guid,my_num,my_price,is_comment')
            ->view('my_guid_all','my_name,my_type,my_price,my_pic','my_orders_access.my_guid=my_guid_all.my_guid')
            ->view('my_common_picture','path','my_common_picture.id=my_guid_all.my_pic')
            ->where('order_no',$order_no)
            ->cache('OrderList_'.$order_no)
            ->select();
    }

}